package com.lovo.bbs.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.bbs.bo.ForumBo;
import com.lovo.bbs.bo.ForumStatBo;
import com.lovo.bbs.bo.PostBo;
import com.lovo.bbs.bo.RankBo;
import com.lovo.bbs.bo.TopicBo;
import com.lovo.bbs.vo.ForumStatVo;
import com.lovo.bbs.vo.ForumVo;
import com.lovo.bbs.vo.PostVo;
import com.lovo.bbs.vo.RankVo;
import com.lovo.bbs.vo.TopicVo;

/**
 * 打开单个主题时,初始化该主题及其回复的数据
 * 
 * @author tiancen2001
 * 
 */
public class InitTopicFilter implements Filter {

	public void destroy() {
	}

	@SuppressWarnings("unchecked")
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;

		//主题ID
		int topicid = Integer.parseInt(req.getParameter("tid"));
		//更新主题查看数,查看数加一
		TopicBo topicBo = new TopicBo();
		topicBo.topicHitted(topicid);
		
		// 取得主题信息
		TopicVo topicInfo = topicBo.getTopicInfo(topicid);
		req.setAttribute("topicInfo", topicInfo);
		
		// 取得所有回帖列表
		ArrayList<PostVo> postList1 = new PostBo().getAllPost(topicid);
		
		// 将主题包装成回帖,在第一页与回帖一起显示
		ArrayList<PostVo> postList2 = new ArrayList<PostVo>();
		PostVo topicToPost = new PostVo();
		topicToPost.setContent(topicInfo.getContent());
		topicToPost.setHeadimg(topicInfo.getAuthorHeadimg());
		topicToPost.setPostdate(topicInfo.getTopicDate());
		topicToPost.setPostid(topicInfo.getTopicid());
		topicToPost.setPosttitle(topicInfo.getTopicTitle());
		topicToPost.setRankid(topicInfo.getAuthorRankid());
		topicToPost.setScore(topicInfo.getAuthorScore());
		topicToPost.setSignature(topicInfo.getAuthorSignature());
		topicToPost.setTopicid(topicInfo.getTopicid());
		topicToPost.setUserid(topicInfo.getAuthorid());
		topicToPost.setUsername(topicInfo.getAuthorname());
		topicToPost.setUserTopicNum(topicInfo.getUserTopicNum());
		postList2.add(topicToPost);
		postList2.addAll(postList1);

		// 总帖子数(总回帖数加一)
		int postNum = postList2.size();
		req.setAttribute("postNum", Integer.valueOf(postNum));

		// 请求页码
		String queryPage = request.getParameter("page");
		if (queryPage == null) {
			queryPage = "1";
		}
		req.setAttribute("queryPage", Integer.valueOf(queryPage));

		// 取出需要在请求页显示的帖子(内存分页)
		ArrayList<PostVo> postList3 = new ArrayList<PostVo>();
		int fromIndex = (Integer.parseInt(queryPage) - 1) * 20;
		int toIndex = fromIndex + 20;
		if (toIndex > postList2.size()) {//防止越界
			toIndex = postList2.size();
		}
		postList3.addAll(postList2.subList(fromIndex, toIndex));
		req.setAttribute("postList", postList3);

		// 取得RankMap
		ServletContext context = req.getSession().getServletContext();
		HashMap<Integer, RankVo> rankMap = (HashMap<Integer, RankVo>) req
				.getSession().getServletContext().getAttribute("rankMap");
		// 更新RankMap
		if (rankMap == null) {
			rankMap = new RankBo().getRankMap();
		}
		req.setAttribute("rankMap", rankMap);

		// 取得forumMap
		HashMap<Integer, ForumVo> forumMap = (HashMap<Integer, ForumVo>) context
				.getAttribute("forumMap");
		// 更新forumMap和论坛列表
		ForumBo forumBo = new ForumBo();
		if (forumMap == null) {
			// 取得所有论坛列表
			ArrayList<ForumVo> forumList = forumBo.getAllForum();
			// 更新论坛列表
			context.setAttribute("forumList", forumList);
			// 更新forumMap
			context.setAttribute("forumMap", forumBo.getForumMap(forumList));
		}
		req.setAttribute("forumMap", forumMap);		

		// 取得网站杂项信息
		ForumStatVo forumInfo = (ForumStatVo) context.getAttribute("forumInfo");
		// 更新网站杂项信息
		if (forumInfo == null) {
			forumInfo = new ForumStatBo().getForumInfo();
		}
		context.setAttribute("forumInfo", forumInfo);
		req.setAttribute("forumInfo", forumInfo);

		//记住上一次的主题列表页的页数
		String backpage = req.getParameter("backpage");
		if (backpage == null) {
			backpage = "1";
		}
		req.setAttribute("backpage", backpage);
		
		// 是否定位到最后回帖
		String end = request.getParameter("end");
		int pageNum = (postNum % 20 == 0) ? (postNum / 20) : (postNum / 20 + 1);// 页数
		req.setAttribute("pageNum", Integer.valueOf(pageNum));
		if (end == null) {
			end = "0";
			req.setAttribute("end", end);
		} else if ("1".equals(end)) {//重定向到最后页
			String url="topic.jsp?tid=" + topicid + "&backpage="+ backpage + "&page=" + pageNum ;
			resp.sendRedirect(url);	
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

}
